library FlareSkill initializer init requires LegacySystem


	private function FlareGunConditions takes nothing returns boolean
		local integer i = GetSpellAbilityId()
		return i=='A058' or i=='A00G' or i=='Afla'
	endfunction

	private function FlareGun takes nothing returns nothing
		local unit u
		local player p = GetOwningPlayer(GetTriggerUnit())
		local location l = GetSpellTargetLoc()
		local real x = GetLocationX(l)
		local real y = GetLocationY(l)
		local integer i = 1
		call PolledWait(0.6)
		if udg_Mode==3 then
			set u = CreateUnit(Player(9),'e00M',x,y,0.0)
		elseif udg_LostCause==false then
			set u = CreateUnit(Player(0),'e00M',x,y,0.0)
		endif
		call UnitApplyTimedLife(u,'Bhwd',7.0)
		if udg_Mode==3 then //Deathmatch
			loop
				exitwhen i>udg_InitialPlayers
				if IsPlayerAlly(ConvertedPlayer(udg_Sorted[i]),p) then
					set u = CreateUnit(ConvertedPlayer(udg_Sorted[i]),'e002',x,y,0.0)
					call UnitApplyTimedLife(u,'Bhwd',15.0)
				endif
				//call TriggerSleepAction(0.)
				set i=i+1
			endloop
		else
			loop
				exitwhen i>udg_Players
				set u = CreateUnit(ConvertedPlayer(udg_Sorted[i]),'e002',x,y,0.0)
				call UnitApplyTimedLife(u,'Bhwd',15.0)
				//call TriggerSleepAction(0.)
				set i=i+1
			endloop
		endif
		set u = CreateUnit(Player(9),'e002',x,y,0.0) //Civilians or Patrols in LC
		call UnitApplyTimedLife(u,'Bhwd',15.0)
		call TriggerSleepAction(0.)
		if udg_LostCause then
			set u = CreateUnit(Player(13),'e002',x,y,0.0) //Static Defense in LC
			call UnitApplyTimedLife(u,'Bhwd',15.0)
			call TriggerSleepAction(0.)
		endif
		set u = CreateUnit(Player(0),'e001',x,y,0.0)
		call UnitApplyTimedLife(u,'Bhwd',14.0)
		call RemoveLocation(l)
		set l = null
		set u = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
		call TriggerAddCondition(t, Condition( function FlareGunConditions ) )
		call TriggerAddAction(t,function FlareGun)
	endfunction

endlibrary